<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Centre</title>	
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
	<link rel="stylesheet" type="text/css" href="./jquery/icon.css">
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);
	
	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}
	if ($userType!="System Admin") {
		$url="./Index.php";
		include_once './Tool/Common/Redirect.php';
		exit();
	}
	
	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
?>

<body>
	<div class="banner">
	</div>
	<div id="main">
		<div class="left-wrapper">
			<div class="logo">
				<img src="images/eldercare.gif" alt="logo" width="240px" />
			</div>
			<div class="menu-wrapper">
				<div id="cssmenu">
					<?php
						if ($userType == "Therapy Assistant") {
							include_once('./Sidebar/_SidebarTA.php');
						} else if ($userType == "Centre Manager") {
							include_once('./Sidebar/_SidebarCM.php');
						} else {
							include_once('./Sidebar/_SidebarSA.php');
						}
					?>
				</div>
			</div>
		</div>           
            <div id="content">
                <div class="content2">
                    <b style="float:right; margin: 0px 0 0 0">  
						<?php
							//session_start();
							if (!empty($_SESSION["centreName"])) {
								$Username = $_SESSION["username"];
								$centreName = $_SESSION["centreName"];
								echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
							} else {
								$Username = $_SESSION["username"];
								echo "Welcome, " . $Username;
							}
						?>
						<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
					</b>	
                    </p>
					<br>
					<div style="float:center">
						<br>
                        <h3>Centres</h3>
                        <br>
                        <table id="dg" title="Centres" class="easyui-datagrid" style="width:auto;height:430px; "
                               url="CRUDCentres/get_centres.php"
                               toolbar="#toolbar" pagination="true"
                               rownumbers="true" fitColumns="true" singleSelect="true">
                            <thead>
                                <tr>
                                    <th field="CentreName" width="50" sortable="true">Centre Name</th>
                                    <th field="Address" width="50" sortable="true">Address</th>
                                    <th field="PostalCode" width="50" sortable="true">Postal Code</th>
                            <tbody style="display:none">
                            <th field="CentreId" width="50" >CentreId</th>
                            </tr>
                            </thead>
                        </table>
                        <div id="toolbar">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newCentre()">New Centre</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editCentre()">Edit Centre</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeCentre()">Remove Centre</a>
                            <input id="searchid" name="searchid" type="text" style="border-radius:25px">  
                            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onClick="getCentre()">Search</a>
                            <a href="#" class="easyui-linkbutton" onClick="findAll()">All</a>
                        </div>

                        <!-- Insert Dialog -->

                        <div>
                            <div id="insertdlg" class="easyui-dialog" style="width:400px;height:230px;padding:10px 20px"
                                 closed="true" buttons="#insertdlg-buttons">
                                <div class="ftitle">Centres Information</div>
                                <form id="insertform" method="post" novalidate>
                                    <div class="fitem">
                                        <label>Centre Name: </label>
                                        <input id="InsertCentreName" name="InsertCentreName" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                    </div>
                                    <div class="fitem">
                                        <label>Address: </label>
                                        <input id="InsertAddress" name="InsertAddress"class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                    </div>
                                    <div class="fitem">
                                        <label>Postal Code: </label>
                                        <input id="InsertPostalCode" name="InsertPostalCode" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                    </div>
                                </form>
                            </div>
                            <div id="insertdlg-buttons">
                                <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveCentre()">Save</a>
                                <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#insertdlg').dialog('close')">Cancel</a>
                            </div>
                        </div>		


                        <!-- Update Dialog -->
                        <div>
                            <div id="updatedlg" class="easyui-dialog" style="width:400px;height:330px;padding:10px 20px"
                                 closed="true" buttons="#updatedlg-buttons">
                                <div class="ftitle">Centres Information</div>
                                <form id="updateform" method="post" novalidate>
                                    <div class="fitem">
                                        <label>Centre Name: </label>
                                        <input id="UpdateCentreName" name="CentreName" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                    </div>
                                    <div class="fitem">
                                        <label>Address: </label>
                                        <textarea rows="4" cols="19" id="UpdateAddress" input name="Address" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </textarea>
                                    </div>
                                    <div class="fitem">
                                        <label>Postal Code: </label>
                                        <input id="UpdatePostalCode" name="PostalCode" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                    </div>
                                    <div class="fitem">
                                        <input id="UpdateCentreId" name="CentreId" class="easyui-validatebox" type = "hidden" required="true">
                                    </div>
                                </form>
                            </div>
                            <div id="updatedlg-buttons">
                                <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="updateCentre()">Save</a>
                                <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#updatedlg').dialog('close')">Cancel</a>
                            </div>
                        </div>
                    </div><p>
                </div>
                <div class="clear"></div>
			</div>
	</div>
	<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body>
<script type="text/javascript" src="./jquery/jquery-1.6.1.min.js"></script>       
<script type="text/javascript" src="./jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type="text/javascript">
	var url;
	function newCentre() {
		$('#insertdlg').dialog('open').dialog('setTitle', 'New Centre');
		$('#insertform').form('clear');
		url = 'CRUDCentres/save_centres.php';
	}

	function saveCentre() {
		$('#insertform').form('submit', {
			onSubmit: function() {
				$(this).form('validate');
				//validate input	
				var flagCentreName=0;
				var flagCentreNameExist=0;
				var flagAddress=0;
				var flagPostalCode=0;
				$.ajax({
					async : false,
					type : "POST",
					url : "./CRUDCentres/validateInsert.php", //the page/method you wanna validate
					data : {
						InsertCentreName: $("#InsertCentreName").val(),
						InsertAddress: $("#InsertAddress").val(),
						InsertPostalCode:$("#InsertPostalCode").val()
					},
					success : function(data) {
						var data = JSON.parse(data);
						flagCentreName=data.flagCentreName;
						flagCentreNameExist=data.flagCentreNameExist;
						flagAddress=data.flagAddress;
						flagPostalCode=data.flagPostalCode;
					}
				});
				if(flagCentreName==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Centre Name. Centre Name cannot contain any special characters except brackets.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagCentreNameExist==0){
					$.messager.show({
						title: 'Error',
						msg: 'This Centre Name has already been used! Please enter another Centre Name.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagAddress==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Address. Address cannot contain any special characters except brackets, number signs and dashes.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagPostalCode==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Singapore Postal Code Format.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else{
					return $(this).form('validate');	
				}
			},
			url: './CRUDCentres/save_centres.php',
			success: function(result) {
				var result = eval('(' + result + ')');
				if (result.success) {
					$('#insertdlg').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg,
						showType:'fade',
						style:{
							right:'',
							bottom:''
						}
					});
				}
			}
		});
	}

	function editCentre() {
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$('#updatedlg').dialog('open').dialog('setTitle', 'Edit Centre');
			$('#updateform').form('load', row);
		}
	}

	function updateCentre() {
		$('#updateform').form('submit', {
			onSubmit: function() {
				$(this).form('validate');
				//validate input
				var flagCentreId=0;	
				var flagCentreName=0;
				var flagCentreNameExist=0;
				var flagAddress=0;
				var flagPostalCode=0;
				$.ajax({
					async : false,
					type : "POST",
					url : "./CRUDCentres/validateUpdate.php", //the page/method you wanna validate
					data : {
						UpdateCentreId:$("#UpdateCentreId").val(),
						UpdateCentreName: $("#UpdateCentreName").val(),
						UpdateAddress: $("#UpdateAddress").val(),
						UpdatePostalCode:$("#UpdatePostalCode").val()
					},
					success : function(data) {
						var data = JSON.parse(data);
						flagCentreId=data.flagCentreId;
						flagCentreName=data.flagCentreName;
						flagCentreNameExist=data.flagCentreNameExist;
						flagAddress=data.flagAddress;
						flagPostalCode=data.flagPostalCode;
					}
				});
				if(flagCentreId==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Centre Id. Centre Id cannot contain any special characters and letters.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagCentreName==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Centre Name. Centre Name cannot contain any special characters except brackets.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagCentreNameExist==0){
					$.messager.show({
						title: 'Error',
						msg: 'This Centre Name has already been used! Please enter another Centre Name.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagAddress==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Address.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else if(flagPostalCode==0){
					$.messager.show({
						title: 'Error',
						msg: 'Invalid Singapore Postal Code Format.',
						showType:'fade',
						style:{
		                    right:'',
                    		bottom:''
		                }
					});
					die; 
				}else{
					return $(this).form('validate');	
				}
			},
			url: './CRUDCentres/update_centres.php',
			success: function(result) {
				var result = eval('(' + result + ')');
				if (result.success) {
					$('#updatedlg').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg,
						showType:'fade',
						style:{
							right:'',
							bottom:''
						}
					});
				}
			}
		});
	}

	function removeCentre() {
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$.messager.confirm('Confirm Delete Centre', 'Are you sure you want to remove this centre?', function(r) {
				if (r) {
					//validate Centre Id
					var check = 0;
					$.ajax({
						async : false,
						type : "POST",
						url : "./CRUDCentres/validateRemove.php", //the page/method you wanna validate
						data: {
							CentreId: row.CentreId
						},
						success : function(data) {
							check=data;
						}
					});
					if(check==0){
						$.messager.show({
							title: 'Error',
							msg: 'Invalid Centre ID for Remove',
							showType:'fade',
							style:{
								right:'',
								bottom:''
							}
						});
					}else{
						$.post('./CRUDCentres/remove_centres.php', {CentreId: row.CentreId}, function(result) {
							if (result.success) {
								$('#dg').datagrid('reload');	// reload the user data
							} else {
								$.messager.show({// show error message
									title: 'Error',
									msg: result.msg,
									showType:'fade',
									style:{
										right:'',
										bottom:''
									}
								});
							}
						}, 'json');
					}
				}
			});
		}
	}
	function getCentre() {
        //validate search input    
        var check = 0;
        $.ajax({
            async : false,
            type : "POST",
            url : "./CRUDCentres/validateSearch.php", //the page/method you wanna validate
            data: "searchid=" + $("#searchid").val(),
            success : function(data) {
                check=data;
            }
        });
        if(check==0){
            $.messager.show({
                title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except bracket, number sign, dashes and colons.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
            });
        }else{
            $('#dg').datagrid('load', {
                searchid: $('#searchid').val()
            }); 
        }
    }
	function findAll() {
		$('#dg').datagrid('load', {
		});
	}
</script>
</html>